<?php

namespace app\common\model\distribute;

use common\Common;

class TrainUserCommission extends base
{
    public function getCommissionByPxAdminId()
    {
        $result = [];
        $this->write_file('-----------培训部-----------');
        if (isset($this->orderInfo['px_admin_id']) && $this->orderInfo['px_admin_id']) {
            $pxAdminId = $this->orderInfo['px_admin_id'];
            $pxAdminMoney = $this->orderInfo['px_commission_money'];
            $this->write_file('培训部用户：' . $pxAdminId);
            $this->write_file('培训部用户佣金：' . $pxAdminMoney);
            $findPxAdminRes = $this->admin->findAdmin(['id' => $pxAdminId]);
            if ($findPxAdminRes['errorCode'] == 0) {
                $adminInfo = $findPxAdminRes['data'];
                $data = $this->getData($adminInfo, $pxAdminMoney);
                $this->admin->updateAdmin(['id' => $pxAdminId], $data);
                $sql2 = $this->user->getLastSql();
                $this->write_file('更新会员佣金SQL：' . $sql2);
                // 添加会员佣金记录
                $sql3 = $this->extracted($pxAdminMoney, $adminInfo['balance'], $pxAdminId,3,'培训部');
                $this->write_file('添加佣金记录SQL：' . $sql3);
                $result[] = [
                    'update_admin_balance_sql' => $sql2,
                    'create_admin_commission_record_sql' => $sql3
                ];

                $pxbStaffListRes = $this->admin->findAllAdmin(['bid' => $pxAdminId]);
                if ($pxbStaffListRes['errorCode'] == 0) {
                    array_map(function ($staff) use ($pxAdminMoney) {
                        if (floatval($staff['rate'])) {
                            $staff_commission_money = $staff['rate'] * $pxAdminMoney;
                            $staff_commission_money = (float)sprintf("%.2f", substr(sprintf("%.3f", $staff_commission_money), 0, -1));
                            $data = $this->getData($staff, $staff_commission_money);
                            $this->admin->updateAdmin(['id' => $staff['id']], $data);
                            $this->extracted($staff_commission_money, $staff['balance'], $staff['id'], 5, '培训部员工');
                        }
                    },$pxbStaffListRes['data']);
                }
            } else {
                $this->write_file('用户不存在' );
            }
        }

        return $result;
    }

    private function getData($memberInfo, $commission)
    {
        $total_balance = (float)$memberInfo['total_balance'] + (float)$commission;//累计佣金
        $balance = (float)$memberInfo['balance'] + (float)$commission;//可提现佣金
        $commission = (float)$memberInfo['commission'] + (float)$commission;//冻结佣金
        if ($this->type == 2 || $this->type == 3) {//当订单类型是 【红娘认证】【栏目认证】，所得金额进入冻结佣金字段
            $data['commission'] = $commission;
        } else {
            $data = [
                'total_balance' => $total_balance,
                'balance' => $balance,
            ];
        }
        return $data;
    }

    private function extracted($money, $balance, $userId, $state, $bm = '培训部')
    {
        $sql = '';
        if (floatval($money) > 0) {
            $type = $this->orderInfo['type'];
            $typeStr = '';
            if ($type == 0) $typeStr = '付费看视频';
            if ($type == 1) $typeStr = '购买VIP套餐';
            if ($type == 2) $typeStr = '栏目认证';
            if ($type == 3) $typeStr = '红娘认证';
            $curPayUser = $this->curPayUserInfo;
            if ($curPayUser['errorCode'] == 0) {
                $info = $curPayUser['data']['username'] . $typeStr . '，' . $bm . '可得佣金：' . $money;
            } else {
                $info = $typeStr . '，' . $bm . '可得佣金：' . $money;
            }
            $data = [];
            $data['user_id'] = $userId;
            $data['describe'] = $info;
            $data['before_balance'] = $balance;
            $data['amount'] = $money;
            $data['state'] = $state;
            $data['add_time'] = Common::getTime();
            $data['order_id'] = $this->orderInfo['id'];
            $data['pay_user_id'] = $this->orderInfo['user_id'];
            $data['type'] = $this->type;
            $data['zj_state'] = in_array($this->type, [2, 3]) ? 1 : 0;//佣金状态 0可提现 1冻结中 2已失效
            $data['order_info'] = json_encode($this->orderInfo, JSON_UNESCAPED_UNICODE);
            $this->adminBalanceRecords->addCommissionRecords($data);
            $sql = $this->adminBalanceRecords->getLastSql();
        }
        return $sql;
    }
}